#region Using

using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;

using Gizmox.WebGUI;
using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms.Design;
using Gizmox.WebGUI.Common.Interfaces;
using Gizmox.WebGUI.Common.Extensibility;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Collections;

#endregion

//[DesignTimeController("Controls.CodeTextAreaDesigner, Controls")]
//[ClientController("Controls.CodeTextAreaController, Controls")]

namespace Controls
{
    /// <summary>
    /// Summary description for CodeTextArea
    /// </summary>
    [ToolboxItem(true)]
    [ToolboxBitmapAttribute(typeof(CodeTextArea), "Controls.CodeTextArea.bmp")]
    //[DesignTimeController("Gizmox.WebGUI.Forms.Design.PlaceHolderController, Gizmox.WebGUI.Forms.Design, Version=2.0.5701.0, Culture=neutral, PublicKeyToken=dd2a1fd4d120c769")]
    //[ClientController("Gizmox.WebGUI.Client.Controllers.PlaceHolderController, Gizmox.WebGUI.Client, Version=2.0.5701.0, Culture=neutral, PublicKeyToken=0fb8f99bd6cd7e23")]
    [DesignTimeController("Gizmox.WebGUI.Forms.Design.RichTextBoxController, Gizmox.WebGUI.Forms.Design, Version=2.0.5701.0, Culture=neutral, PublicKeyToken=dd2a1fd4d120c769")]
    [ClientController("Gizmox.WebGUI.Client.Controllers.RichTextBoxController, Gizmox.WebGUI.Client, Version=2.0.5701.0, Culture=neutral, PublicKeyToken=0fb8f99bd6cd7e23")]
    public class CodeTextArea : Control
    {
        private List<CodeTextAreaLine> _lines;

        public event CodeTextAreaCodeChangedEventHandler CodeChanged;

        public CodeTextArea()
        {
            this.TagName = "Controls.CodeTextArea";
        }

        protected void OnCodeChanged(CodeTextAreaCodeChangedEventArgs e)
        {
            if (CodeChanged != null)
                CodeChanged(this, e);
        }

        public List<CodeTextAreaLine> Lines
        {
            get
            {
                if (_lines == null)
                    _lines = new List<CodeTextAreaLine>();

                return _lines;
            }
        }

        protected override void Render(IContext objContext, IResponseWriter objWriter, long lngRequestID)
        {
            foreach (CodeTextAreaLine objLine in this.Lines)
            {
                objWriter.WriteStartElement("VDBTags.Line");

                foreach (CodeTextAreaWord objWord in objLine.Words)
                {
                    objWriter.WriteStartElement("VDBTags.Word");

                    objWriter.WriteAttributeString("WT", objWord.Text);
                    objWriter.WriteAttributeString("WC", ColorTranslator.ToHtml(objWord.TextColor));
                    objWriter.WriteAttributeString("WF", Utility.GetWebFont(objWord.TextFont));

                    objWriter.WriteEndElement();
                }

                objWriter.WriteEndElement();
            }
        }

        protected override void RenderAttributes(IContext context, IAttributeWriter writer)
        {
            base.RenderAttributes(context, writer);

            writer.WriteAttributeString(WGAttributes.Text, this.Text);
        }

        public override string Text
        {
            get
            {
                return GetCommandText(base.Text);
            }
            set
            {
                string strValue = value == null ? "" : value;
                if (base.Text != strValue)
                {
                    base.Text = strValue;
                    CodeTextAreaCodeChangedEventArgs eventArgs = new CodeTextAreaCodeChangedEventArgs();
                    OnCodeChanged(eventArgs);
                    this.Update();
                }
            }
        }

        protected override void FireEvent(IEvent objEvent)
        {
            switch (objEvent.Type)
            {
                case "CodeChanged":
                    this.Text = objEvent[WGAttributes.Text];
                    break;

                default:
                    base.FireEvent(objEvent);
                    break;
            }
        }
    }

    public class CodeTextAreaLine
    {
        List<CodeTextAreaWord> _words;

        public List<CodeTextAreaWord> Words
        {
            get
            {
                if (_words == null)
                    _words = new List<CodeTextAreaWord>();

                return _words;
            }
        }
    }

    public class CodeTextAreaWord
    {
        private string _text;
        private Color _textColor;
        private Font _textFont;

        public CodeTextAreaWord()
        {
        }

        public string Text
        {
            get
            {
                return _text;
            }

            set
            {
                _text = value;
            }
        }

        public Color TextColor
        {
            get
            {
                return _textColor;
            }

            set
            {
                _textColor = value;
            }
        }

        public Font TextFont
        {
            get
            {
                return _textFont;
            }

            set
            {
                _textFont = value;
            }
        }
    }

    public delegate void CodeTextAreaCodeChangedEventHandler(object sender, CodeTextAreaCodeChangedEventArgs e);

    public class CodeTextAreaCodeChangedEventArgs : EventArgs
    {
    }
}